<?php
	class DB_Sqlite3{
		private static $_instance;
		protected $_db; 
		public $db_path;
		private function __construct(){
			$this->db_path = dirname(dirname(dirname(dirname(__FILE__)))).'/static/13duser.sqlite.sqlite';
			$this->_db = new SQLite3($this->db_path);
		}
		private function __clone(){}
		public function initialize(){
			if(empty(self::$_instance)){    
				self::$_instance = new self();    
			}  

			return self::$_instance;    
		}
		public function executed($sql){//对数据库执行insert update delete语句
			return $this->_db->exec($sql);
		}
		public function getLastRowId(){ //返回操作sqlite时插入数据库中最近插入行的ID
			return $this->_db->lastInsertRowID();
		}
		public function getChanges(){
			return $this->_db->changes();
		}
		public function query($sql){//对数据库执行sql查询语句
			return $this->_db->query($sql);
		}
		public function select($table,$where=null,$return='*',$order='',$limit=''){ //查询数据库
			$w = ' where 1=1';
			if(!empty($where)){
				foreach($where as $k=>$v){
					$w .= ' and '.$k.$v;
				}
			}
			$sql = 'select '.$return.' from '.$table.$w;
			if(!empty($order)){
				$sql .=' order by '.$order;
			}
			if(!empty($limit)){
				$sql .=' limit '.$limit;
			}
			$re = $this->_db->query($sql);
			$ret = array();
			while ($row = $re->fetchArray()){
				$ret[] = $row;
			}  
			return $ret;
		}
		public function error(){
			return 'Error Info:('.$this->_db->laseErrorCode().')'.$this->_db->lastErrorMsg();
		}
		private function __destruct(){
			$this->_db->close();
		}
	}